<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2019-01-10 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-05-30 20:18:58
 */

namespace app\mobmall_manage\controller;
use app\common\controller\Backend;

class Navigation extends Backend {

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '导航菜单', '', 'navigation');

		$this->navigationDao = model('Navigation');
	}

	public function index() {
		list($map, $parameter) = $this->getMap();

		$count = $this->navigationDao->countSearch($map);
		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		$pageShow = $Page->show();
		list($list) = $this->navigationDao->search($map, $Page->getLimit(), $orderby);
		$this->assign("count", $count);
		$this->assign('pageShow', $pageShow);
		$this->assign("list", $list);
		
		return $this->fetch();
	}

	private function getMap() {
		$map = $parameter = array();

		$status = input('param.status', '', 'pwEscape');
		if ($status) {
			$map['status'] = $status;
			$parameter['status'] = $status;
		}
		$this->assign('status', $status);

		$name = input('param.name', '', 'pwEscape');
		if ($name) {
			$map['name'] = array('like', '%'.$name.'%');
			$parameter['name'] = $name;
		}
		$this->assign('name', $name);

		return array($map, $parameter);
	}

	public function listorders() {
		$result = parent::listordersOrigin($this->navigationDao);
		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败'.showDbError($this->navigationDao));
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->navigationDao->getInfo($id);
		} else {
			$info = [
				'status'	=> 1
			];
		}
		$this->assign('id', $id);
		$this->assign('info', $info);
		
		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if ($id) {
			$info = $this->navigationDao->get($id);
		}

		//普通上传图片
		$uploadInfo = $this->comuploadFile('navigation', 'picurl', $info['picurl']);
		$uploadInfo && $data['picurl'] = $uploadInfo['saveFullPath'];
		
		if ($id) {
			$result = $this->navigationDao->baseUpdateData($id, $data);
			if ($result !== false) {
				$this->clearCache(); //清除缓存
				$this->success('保存成功', url('mobmall_manage/navigation/index'));
			} else {
				$this->error('保存失败'.showDbError($this->navigationDao));
			}
		} else {
			$data['status'] = 1;
			$data['create_time'] = $this->ts;
			$result = $id = $this->navigationDao->baseAddData($data);
			if ($result !== false) {
				$this->clearCache(); //清除缓存
				$this->success('添加成功', url('mobmall_manage/navigation/index'));
			} else {
				$this->error('添加失败'.showDbError($this->navigationDao));
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');

		$result = $this->navigationDao->delInfo($id);
		if ($result !== false) {
			$this->clearCache(); //清除缓存
			$this->success('删除成功', url('mobmall_manage/navigation/index'));
		} else {
			$this->error('删除失败'.showDbError($this->navigationDao));
		}
	}

	private function clearCache() {
		$sys_identify = 'navigation';
		cache($sys_identify, NULL); //删除缓存数据
	}
	
}